package com.csair.seam.domain.service.impl;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csair.seam.domain.mapper.FaFinanceMapper;
import com.csair.seam.domain.model.entity.FaAsset;
import com.csair.seam.domain.model.entity.FaEquip;
import com.csair.seam.domain.model.entity.FaEquipRatio;
import com.csair.seam.domain.model.entity.FaFinance;
import com.csair.seam.domain.model.param.FaAssetBO;
import com.csair.seam.domain.model.param.FaEquipParam;
import com.csair.seam.domain.service.IFaFinanceService;
import com.csair.seam.infrastructure.config.DictionaryConfigurationProperties;

@Service
public class FaFinanceServiceImpl extends ServiceImpl<FaFinanceMapper, FaFinance> implements IFaFinanceService {
	
	@Autowired
    private RestTemplate restTemplate;
	
	@Autowired
	private DictionaryConfigurationProperties dictionaryConfigurationProperties;

	@Override
	public void addFaFinanceList() {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.add("Authorization", dictionaryConfigurationProperties.getFaToken());
		SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
	    factory.setReadTimeout(30*60*1000);//单位为ms
	    restTemplate.setRequestFactory(factory);
		
		List<String> faEquipCodeList = getBaseMapper().findFaEquipCodeList();
		for (String faEquipCode : faEquipCodeList) {
			FaAssetBO bo = new FaAssetBO();
			bo.setSourceNumber(faEquipCode);
			String json = JSON.toJSONString(bo);
			HttpEntity<String> formEntity = new HttpEntity<String>(json, headers);
			String result = restTemplate.postForObject(dictionaryConfigurationProperties.getFaUrl(), formEntity, String.class);
			List<FaAsset> list = JSONObject.parseArray(result, FaAsset.class);
			if(CollectionUtils.isNotEmpty(list)) {
				for (FaAsset faAsset : list) {
					FaFinance faFinance = new FaFinance();
					faFinance.setEquipCode(faAsset.getAssetNumber());
					faFinance.setFaFlag("Y");
					getBaseMapper().insert(faFinance);
				}
			}
		}
		
	}

	@Override
	public FaEquipRatio findFaEquipRatio(FaEquipParam param) {
		Page<FaEquip> page = new Page<FaEquip>(param.getPageIndex(), param.getPageSize());
		page =getBaseMapper().findFaEquipList(page, param);
		if(page.getTotal() == 0) {
			FaEquipRatio ratio = new FaEquipRatio();
			ratio.setTotalNum(0);
			ratio.setTransferredNum(0);
			ratio.setTransferredRatio(0D);
			ratio.setUnTransferNum(0);
			ratio.setUnRransferRatio(0D);
			return ratio;
		}
		return getBaseMapper().findFaEquipRatio(param);
	}

	@Override
	public Page<FaEquip> findFaEquipPage(FaEquipParam param) {
		Page<FaEquip> page = new Page<FaEquip>(param.getPageIndex(), param.getPageSize());
		page =getBaseMapper().findFaEquipList(page, param);
		return page;
	}

	@Override
	public List<FaEquip> findFaEquipList(FaEquipParam param) {
		return getBaseMapper().findFaEquipList(param);
	}
	
	
	
}
